﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Framwork.Winform.Test
{
    public partial class FrmSimilarRx : Form
    {
        private string AllergyInfo { get; set; }
        private string DiagnoseInfo { get; set; }
        private string DrugInfo { get; set; }
        public FrmSimilarRx()
        {
            InitializeComponent();

        }

        public void InitDate(SimilarRxModel model)
        {
            HandleAllergyInfo(model.AlleryInfo);
            HandleDiagnoseInfo(model.DiagnoseInfo);
            HandleDrugInfo(model.ContaintDrinkDrug,model.DrugInfo);
            //给处方赋值
            labRxId.Text = model.RxId;
            labRxType.Text = model.RxType;
            labHospitalName.Text = model.HospitalName + "处方笺";
            labPatientName.Text = model.PatientName;
            labGender.Text = model.PatientGender;
            labAge.Text = model.PatientAge;
            chargeType.Text = model.ChargeTpye;
            labdjNo.Text = model.DjNo;
            lablsNo.Text = model.RxId;
            labPatientWight.Text = model.PatientWeight;
            labRxIdent.Text = model.RxIdent;
            labCarNo.Text = model.PatientCareNo;
            labRxDate.Text = model.RxDate;
            labRxDepartment.Text = model.Department;
            labAllergyInfo.Text = AllergyInfo;
            labDiagnaseInfo.Text = DiagnoseInfo;
            labRxDrugInfo.Text = DrugInfo;
            labPhysicianName.Text = model.PhysicianName;
            labHdPerson.Text = model.PhysicianName;
            labMoney.Text = model.ChargeSum;
            labHdPerson.Text = model.AuditPerson;
            labFyPerson.Text = model.DispensePerson;
            //labZYsyffInfo.Text = model.UsageInfo;
            ChangeRxModelColor(labRxType.Text);
        }

        private void FrmSimilarRx_Load(object sender, EventArgs e)
        {
            var formWith = this.Width;
            var rxWith = 500;
            panalLeft.Width = (formWith - rxWith) / 2;
            panalRight.Width = (formWith - rxWith) / 2;
        }

        private void FrmSimilarRx_SizeChanged(object sender, EventArgs e)
        {
            var formWith = this.Width;
            var rxWith = 500;
            panalLeft.Width = (formWith - rxWith) / 2;
            panalRight.Width = (formWith - rxWith) / 2;
        }

        /// <summary>
        /// 处理过敏信息
        /// </summary>
        /// <param name="dt"></param>
        public void HandleAllergyInfo(DataTable dt)
        {
            AllergyInfo = string.Empty;
            if (dt is null || dt.Rows.Count == 0)
            {
                AllergyInfo = "";
                return;
            }
            foreach (DataRow item in dt.Rows)
            {
                if (string.IsNullOrWhiteSpace(item[0]?.ToString())) continue;
                AllergyInfo += item[0]?.ToString() + ";";
            }

        }


        /// <summary>
        /// 处理诊断信息
        /// </summary>
        /// <param name="dt"></param>
        public void HandleDiagnoseInfo(DataTable dt)
        {
            DiagnoseInfo = string.Empty;
            if (dt is null || dt.Rows.Count == 0)
            {
                DiagnoseInfo = "";
                return;
            }
            foreach (DataRow item in dt.Rows)
            {
                if (string.IsNullOrWhiteSpace(item[0]?.ToString())) continue;
                DiagnoseInfo += item[0]?.ToString() + ";";
            }
        }

        public void HandleDrugInfo(string isDrinkDrug,DataTable dt)
        {
            DrugInfo = string.Empty;
            if (dt is null || dt.Rows.Count == 0)
            {
                DrugInfo = "";
                return;
            }
            if (isDrinkDrug == "是")
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DrugInfo += $"{i + 1}.{dt.Rows[i]["药品名称"].ToString()} {dt.Rows[i]["每次使用剂量"].ToString()}  ";
                    //labZYsyffInfo.Text += $@"{dt.Rows[i]["给药途径"]?.ToString()} X {dt.Rows[i]["开药数量"]?.ToString()};";
                    labZYsyffInfo.Text = "煎、服法：";
                }
            }
            else
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DrugInfo += $@"{i + 1}.{dt.Rows[i]["药品名称"].ToString()} X {dt.Rows[i]["开药数量"].ToString()}
                    用法：每次{dt.Rows[i]["每次使用剂量"].ToString()} {dt.Rows[i]["给药途径"].ToString()} {dt.Rows[i]["频次"].ToString()}  {System.Environment.NewLine}";
                    labZYsyffInfo.Text = "";
                }
            }
            
        }

        public void ChangeRxModelColor(string rxType)
        {
            switch (rxType)
            {
                case var type when string.IsNullOrWhiteSpace(rxType) : 
                     panalFull.Appearance.BackColor = Color.White;
                    break;
                case var type when rxType.Contains("儿科"):
                    panalFull.Appearance.BackColor = Color.FromArgb(153, 255, 153);
                    break;
                case var type when rxType.Contains("普通") || rxType.Contains("精二"):
                    panalFull.Appearance.BackColor = Color.White;
                    break;
                case var type when rxType.Contains("急诊"):
                    panalFull.Appearance.BackColor = Color.FromArgb(252, 255, 51);
                    break;
                case var type when rxType.Contains("麻") || rxType.Contains("精一") || rxType.Contains("急诊"):
                    panalFull.Appearance.BackColor = Color.FromArgb(255, 105, 180);
                    break;
                default:
                    break;
            }
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            ultraButton1.Visible = false;//在打印时隐藏按钮
            this.printDialog1.Document = this.printDocument1;
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
            ultraButton1.Visible = true;//打印完成回复按钮显示
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap _NewBitmap = new Bitmap(panalFull.Width, panalFull.Height);
            panalFull.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
        }
    }
}
